import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
from IPython.display import HTML, Video
from moviepy.editor import VideoFileClip
from PerspectiveTransformation import *
from LaneLines import *
images = []
images.append(mpimg.imread('./Project_data/test_images/straight_lines1.jpg'))
images.append(mpimg.imread('./Project_data/test_images/straight_lines2.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test1.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test2.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test3.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test4.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test5.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test6.jpg'))
def plot(img):
hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
f = plt.figure(figsize=(24, 9))
ax1 = f.add_subplot(1, 3, 1)
ax1.imshow(hls[:,:,0], cmap='gray', vmin=0, vmax=255)
ax2 = f.add_subplot(1, 3, 2)
ax2.imshow(hls[:,:,1], cmap='gray', vmin=0, vmax=255)
ax3 = f.add_subplot(1, 3, 3)
ax3.imshow(hls[:,:,2], cmap='gray', vmin=0, vmax=255)
plt.figure()
plt.imshow(img)
img = images[3]
birdeye = PerspectiveTransformation()
plt.imshow(img)
<matplotlib.image.AxesImage at 0x15364685e80>
plot(img)
def threshold_rel(img, lo, hi):
vmin = np.min(img)
vmax = np.max(img)
vlo = vmin + (vmax - vmin) * lo
vhi = vmin + (vmax - vmin) * hi
return np.uint8((img >= vlo) & (img <= vhi)) * 255
def threshold_abs(img, lo, hi):
return np.uint8((img >= lo) & (img <= hi)) * 255
def test(img):
tmp = birdeye.forward(img)
tmp = np.copy(img)
hls = cv2.cvtColor(tmp, cv2.COLOR_RGB2HLS)
hsv = cv2.cvtColor(tmp, cv2.COLOR_RGB2HSV)
h_channel = hls[:,:,0]
l_channel = hls[:,:,1]
s_channel = hls[:,:,2]
v_channel = hsv[:,:,2]
f = plt.figure(figsize=(24, 16))
ax1 = f.add_subplot(2, 2, 1)
ax1.imshow(tmp)
ax2 = f.add_subplot(2, 2, 2)
ax2.imshow(threshold_rel(l_channel, 0.8, 1.0), vmin=0, vmax=255, cmap='gray')
ax3 = f.add_subplot(2, 2, 3)
ax3.imshow(threshold_rel(s_channel, 0.5, 0.8), vmin=0, vmax=255, cmap='gray')
ax4 = f.add_subplot(2, 2, 4)
res = threshold_abs(h_channel, 20, 30)
res &= threshold_rel(v_channel, 0.7, 1.0)
ax4.imshow(res, vmin=0, vmax=255, cmap='gray')
test(images[1])
test(images[2])
test(images[3])
test(images[4])
img = images[1]
plt.imshow(img)
<matplotlib.image.AxesImage at 0x1536680fdf0>
img1 = birdeye.forward(img)
plt.imshow(img1)
<matplotlib.image.AxesImage at 0x15366da45e0>
hls = cv2.cvtColor(img1, cv2.COLOR_RGB2HLS)
hsv = cv2.cvtColor(img1, cv2.COLOR_RGB2HSV)
h_channel = hls[:,:,0]
l_channel = hls[:,:,1]
s_channel = hls[:,:,2]
v_channel = hsv[:,:,2]
f = plt.figure(figsize=(24, 16))
ax1 = f.add_subplot(2, 2, 1)
ax1.imshow(img1)
ax2 = f.add_subplot(2, 2, 2)
right_lane = threshold_rel(l_channel, 0.8, 1.0)
ax2.imshow(right_lane, vmin=0, vmax=255, cmap='gray')
ax3 = f.add_subplot(2, 2, 3)
left_lane = threshold_abs(h_channel, 20, 30)
left_lane &= threshold_rel(v_channel, 0.7, 1.0)
left_lane[:,550:] = 0
ax3.imshow(left_lane, vmin=0, vmax=255, cmap='gray')
img2 = left_lane | right_lane
ax4 = f.add_subplot(2, 2, 4)
ax4.imshow(img2, vmin=0, vmax=255, cmap='gray')
<matplotlib.image.AxesImage at 0x15366ea3d00>
lanelines = LaneLines()
img3 = lanelines.forward(img2)
plt.imshow(img3)
<matplotlib.image.AxesImage at 0x15366fc4c70>
img4 = birdeye.backward(img3)
out_img = cv2.addWeighted(img, 1, img4, 0.3, 0)
plt.imshow(out_img)
<matplotlib.image.AxesImage at 0x15366dc1fd0>
birdeye = PerspectiveTransformation()
lanelines = LaneLines()
def process_image(img):
# step 1
img1 = birdeye.forward(img)
# img1 = np.copy(img)
# step 2
hls = cv2.cvtColor(img1, cv2.COLOR_RGB2HLS)
hsv = cv2.cvtColor(img1, cv2.COLOR_RGB2HSV)
h_channel = hls[:,:,0]
l_channel = hls[:,:,1]
s_channel = hls[:,:,2]
v_channel = hsv[:,:,2]
right_lane = threshold_rel(l_channel, 0.8, 1.0)
right_lane[:,:750] = 0
left_lane = threshold_abs(h_channel, 20, 30)
left_lane &= threshold_rel(v_channel, 0.7, 1.0)
left_lane[:,550:] = 0
img2 = left_lane | right_lane
# step 3
img3 = lanelines.forward(img2)
# step 4
img4 = birdeye.backward(img3)
out_img = cv2.addWeighted(img, 1, img4, 1, 0)
return out_img
tmp = process_image(images[5])
plt.imshow(tmp)
<matplotlib.image.AxesImage at 0x1536679e5b0>
tmp = process_image(images[4])
plt.imshow(tmp)
<matplotlib.image.AxesImage at 0x153668ba970>
tmp = process_image(images[0])
plt.imshow(tmp)
<matplotlib.image.AxesImage at 0x15366689b20>